package com.ccp.dev.im.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.im.model.ImUser;
import com.ccp.dev.im.service.ImUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 在线聊天用户 信息操作处理控制器
 *
 * @author y
 * @date 2020-02-17 11:39:36
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "在线聊天用户")
@RequestMapping("/im/imUser")
public class ImUserController extends SysBaseController {
    @Resource
    private ImUserService imUserService;

    /**
     * 功能描述: 跳转到用户信息页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/userInfo")
    @Action(description = "跳转到用户详情页面", exectype = "页面跳转日志")
    public String userInfo(HttpServletRequest request, ModelMap modelMap) {
        String userId = RequestUtil.getString(request, "userId");
        modelMap.put("userId", userId);
        return "/functionPage/userfun/userinfo";
    }

    /**
     * 功能描述:  修改在线聊天用户的签名
     *
     * @param: request 请求
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping("/updateSign")
    @ResponseBody
    @Action(description = "修改在线聊天用户的签名", detail = "修改在线聊天用户的签名<#if success>成功<#else>失败</#if>")
    public Object updateSign(HttpServletRequest request) {
        String sign = RequestUtil.getString(request, "sign");
        String userId = ContextUtil.getCurrentUserId();
        ImUser imUser = imUserService.getByUserId(userId);
        imUser.setId(imUser.getId());
        imUser.setSign(sign);
        //修改操作
        int result = imUserService.update(imUser);
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
        }
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }


    /**
     * 功能描述: 查询用户详情
     *
     * @param: roomId 用户ID
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description = "查询用户详情", exectype = "查询日志", detail = "查询用户详情<#if success>成功<#else>失败</#if>")
    public Object getUserInfo(@RequestParam("userId") String userId) {
        ImUser imUser = imUserService.getOneById(userId);
        if (null == imUser) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("get.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("get.success", imUser);
    }

    /**
     * 功能描述: 修改用户昵称,头像等信息
     *
     * @param: imUser 用户对象
     * @return: java.lang.Object
     * @auther: y
     * @date: 2020-02-17 11:39:36
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    @Action(description = "修改用户信息", exectype = "更新日志", detail = "修改用户信息<#if success>成功<#else>失败</#if>")
    public Object updateUserInfo(@RequestBody ImUser imUser) {
        if (BeanUtils.isEmpty(imUser) || StringUtil.isEmpty(imUser.getId())) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        try {
            imUserService.updateUserInfo(imUser);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        return getResultDataSuccess("operation.success");
    }
}
